package com.rxt.thermopro.bean

import com.rxt.ble.data.BleDevice
import com.rxt.thermopro.app.LocalDataDao

data class ProbeBean(
    var name: String = "",
    var address: String = "",
    var temperatureNUm: String = "",
    var highTemperatureNUm: String = "",
    var lowTemperatureNUm: String = "",
    val bleDevice: BleDevice? = null,
    var voltage: String = "",
    var alarm: String = "",
    var alarmTrip: Boolean = false,
    var countdown: String = "",
    var batteryNum: String = "",
    var humidityNum: String = "",
    var heatIndex: String = ""
) {


    fun formatProbe2By960(probeMsg: String): ProbeBean {
        val temperatureNUm = (probeMsg.substring(10, 12) +
                probeMsg.substring(8, 10)).toLong(16).toInt() - 30
        this.temperatureNUm =
            if (temperatureNUm >= 65280) "" else "$temperatureNUm"
        val v = (probeMsg.substring(14, 16) +
                probeMsg.substring(12, 14)).toLong(16).toFloat()
        this.voltage = (v / 1000f).toString()
        return this
    }

    /**
     * 10003a00960b
     */
    fun formatProbe1By960(probeMsg: String): ProbeBean {
        val temperatureNUm = (probeMsg.substring(6, 8) +
                probeMsg.substring(4, 6)).toLong(16).toInt() - 30
        this.temperatureNUm =
            if (temperatureNUm >= 65280) "" else "$temperatureNUm"
        val v = (probeMsg.substring(10, 12) +
                probeMsg.substring(8, 10)).toLong(16).toFloat()
        this.voltage = (v / 1000f).toString()
        return this
    }

    /**
     * 10003a00960b
     */
    fun formatProbe1By359(probeMsg: String): ProbeBean {
        val temperatureNUm = (probeMsg.substring(8, 10) + probeMsg.substring(6, 8)).toLong(16)
            .toFloat() / 10
        this.temperatureNUm =
            if (temperatureNUm >= 65280) "" else "$temperatureNUm"
        this.humidityNum = probeMsg.substring(10, 12).toLong(16).toString()
        //0.5 * {t + 61.0 + [（t-68.0）*1.2] + （rh*0.094）}
        val tempF=LocalDataDao.INSTANCE.transFloatUnitCToF(this.temperatureNUm).toFloat()
        this.heatIndex=(0.5* (tempF+61.0+((tempF-68.0)*1.2)+(this.humidityNum.toFloat()*0.094))).toString()
        this.heatIndex=LocalDataDao.INSTANCE.transFloatUnitFToC(this.heatIndex)
        return this
    }


    fun formatProbe1ByGrill(probeMsg: String): ProbeBean {
        val temperatureNUm =
            probeMsg.substring(4, 8).toLong(16).toInt()
        val alarmTrip =
            probeMsg.substring(14, 16).toLong(16).toInt() == 1
        this.temperatureNUm =
            if (temperatureNUm >= 65280) "" else "$temperatureNUm"
        this.alarmTrip = alarmTrip
        val electricityStr = probeMsg.substring(probeMsg.length - 8, probeMsg.length - 6)
        this.batteryNum = electricityStr
        return this
    }

    fun formatProbe2ByGrill(probeMsg: String): ProbeBean {
        val temperatureNUm =
            probeMsg.substring(8, 12).toLong(16).toInt()
        val alarmTrip =
            probeMsg.substring(14, 16).toLong(16).toInt() == 1
        this.temperatureNUm =
            if (temperatureNUm >= 65280) "" else "$temperatureNUm"
        this.alarmTrip = alarmTrip
        val electricityStr = probeMsg.substring(probeMsg.length - 8, probeMsg.length - 6)
        this.batteryNum = electricityStr
        return this
    }

    fun formatProbe1ByBBQ(probeMsg: String): ProbeBean {
        val temperatureNUm =
            probeMsg.substring(4, 8).toLong(16).toInt()
        val alarmTrip =
            probeMsg.substring(22, 24).toLong(16).toInt() == 1
        this.temperatureNUm =
            if (temperatureNUm >= 65280) "" else "$temperatureNUm"
        this.alarmTrip = alarmTrip
        val electricityStr = probeMsg.substring(probeMsg.length - 8, probeMsg.length - 6)
        this.batteryNum = electricityStr
        return this
    }

    fun formatProbe2ByBBQ(probeMsg: String): ProbeBean {
        val temperatureNUm =
            probeMsg.substring(8, 12).toLong(16).toInt()
        val alarmTrip =
            probeMsg.substring(22, 24).toLong(16).toInt() == 1
        this.alarmTrip = alarmTrip
        this.temperatureNUm =
            if (temperatureNUm >= 65280) "" else "$temperatureNUm"
        val electricityStr = probeMsg.substring(probeMsg.length - 8, probeMsg.length - 6)
        this.batteryNum = electricityStr
        return this
    }

    fun formatProbe3ByBBQ(probeMsg: String): ProbeBean {
        val temperatureNUm =
            probeMsg.substring(12, 16).toLong(16).toInt()
        val alarmTrip =
            probeMsg.substring(22, 24).toLong(16).toInt() == 1
        this.temperatureNUm =
            if (temperatureNUm >= 65280) "" else "$temperatureNUm"
        this.alarmTrip = alarmTrip
        val electricityStr = probeMsg.substring(probeMsg.length - 8, probeMsg.length - 6)
        this.batteryNum = electricityStr
        return this
    }

    fun formatProbe4ByBBQ(probeMsg: String): ProbeBean {
        val temperatureNUm =
            probeMsg.substring(16, 20).toLong(16).toInt()
        val alarmTrip =
            probeMsg.substring(22, 24).toLong(16).toInt() == 1
        this.temperatureNUm =
            if (temperatureNUm >= 65280) "" else "$temperatureNUm"
        this.alarmTrip = alarmTrip
        val electricityStr = probeMsg.substring(probeMsg.length - 8, probeMsg.length - 6)
        this.batteryNum = electricityStr
        return this
    }
}